<?php
namespace App\Models;

use Carbon\Carbon;

class HouseDistrictCosts extends CommonModel {
	
	public static function createOrderNo(){
		$order_no = '9'.date('YmdHis').str_pad(rand(1000,9999),6,'0',STR_PAD_LEFT);
		return $order_no;
	}
	
	// 只查询待缴的
	public function scopeUnpaid($query){
		$query->where(['pay_status'=>0]);
	}
	
	// 只查询已缴的
	public function scopePaid($query){
		$query->where(['pay_status'=>1]);
	}
	
	// 费用列表
	public function scopeField($query){
		$query->select('id','house_id','type','type_name','description','start_time','end_time','amount','pay_status','created_at')->orderBy('id','asc');
	}
	
	// 这个格式化比较特殊，所以单独命名
	public static function parseList($data){
		$return = [];
		$data->each(function($v,$k) use(&$return){
			$return[$v->type]['type'] = $v->type;
			$return[$v->type]['name'] = $v->type_name;
			$return[$v->type]['costs'][] = ['id'=>$v->id,'name'=>$v->type_name,'description'=>$v->description,'start_time'=>time2date($v->start_time),'start_date'=>time2date_date($v->start_time),'end_time'=>time2date($v->end_time),'end_date'=>time2date_date($v->end_time),'amount'=>$v->amount];
		});
		return array_values($return);
	}
}